package www.com.library.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;

import www.com.common.library.R;
import www.com.library.app.AppActivities;
import www.com.library.app.AppContances;
import www.com.library.dialog.ToastPopWindow;

/**
 * 网络监控类
 * @author Administrator
 *
 */
public class NetworkMonitor {

	/**
	 * 判断网络是否已连接
	 * @param context
	 * @return
	 */
	public static boolean isNetworkReady(Context context) {
		if(null == context){
			return true;
		}
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
		if (activeNetwork == null)
			return false;
		return activeNetwork.isConnected() && activeNetwork.isAvailable();
	}

	/**
	 * 判断是否连接网络，无网络连接给予提示信息
	 * @return
	 */
	public static boolean hasNetWork() {
		Activity activity = AppActivities.getSingleton().currentActivity();
		if(null == activity){
			return false;
		}
		if (isNetworkReady(activity)) {
			return true;
		}
		showToastPopWindow(activity, activity.getString(R.string.network_msg));
		return false;
	}

	/**
	 * 气泡提示
	 * @param errorMessage
	 */
	public static void showToastPopWindow(Activity activity, String errorMessage){
		new ToastPopWindow(activity, errorMessage).show();
	}

	/**
	 * 判断是否有网络，不提示toast
	 * @return
	 */
	public static boolean hasNetWorkNoToast() {
		if (isNetworkReady(AppActivities.getSingleton().currentActivity())) {
			return true;
		}
		return false;
	}
	
	public static String getNetWorkip() {
		 String ip = "";
		 final ConnectivityManager connMgr = (ConnectivityManager)AppActivities.getSingleton().currentActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
         
	       final NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
	       final NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
	  
	       if(wifi.isAvailable()){
	    	 //获取wifi服务  
	           WifiManager wifiManager = (WifiManager) AppActivities.getSingleton().currentActivity().getSystemService(Context.WIFI_SERVICE);
	           //判断wifi是否开启  
	           WifiInfo wifiInfo = wifiManager.getConnectionInfo();       
	           int ipAddress = wifiInfo.getIpAddress(); 
	           ip  = intToIp(ipAddress);
	       }else if(mobile.isAvailable()){
	           ip = getLocalIpAddress();  
	       }
		return ip;
	}

	private static String getLocalIpAddress() { 
            try { 
                for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { 
                    NetworkInterface intf = en.nextElement(); 
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { 
                        InetAddress inetAddress = enumIpAddr.nextElement(); 
                        if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { 
                            // if (!inetAddress.isLoopbackAddress() && inetAddress 
                            // instanceof Inet6Address) { 
                            return inetAddress.getHostAddress();
                        } 
                    } 
                } 
            } catch (Exception e) { 
            } 
            return ""; 
    }

	private static String intToIp(int ipAddress) {
		 return (ipAddress & 0xFF ) + "." +
	        ((ipAddress >> 8 ) & 0xFF) + "." +       
	        ((ipAddress >> 16 ) & 0xFF) + "." +       
	        ( ipAddress >> 24 & 0xFF) ;  
	}

	public static int getNetworkType(Context activity)
	{
		int strNetworkType = AppContances.NET_WORK_NO_TYPE;
		if(null == activity){
			return strNetworkType;
		}
		ConnectivityManager cm = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
		if(null == cm){
			return strNetworkType;
		}
		NetworkInfo networkInfo = cm.getActiveNetworkInfo();
		if (networkInfo != null && networkInfo.isConnected())
		{
			if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
			{
				strNetworkType = AppContances.NET_WORK_WIFI_TYPE;
			}
			else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
			{
				strNetworkType = AppContances.NET_WORK_MOBILE_TYPE;
			}

			Log.e("cocos2d-x", "Network strNetworkType : " + strNetworkType);
		}

		return strNetworkType;
	}
}
